package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.SnowFlake;
import com.example.demo.dto.Gkpaperorder.GkpaperorderQueryRequest;
import com.example.demo.dto.Jkpaperorder.JkpaperorderQueryRequest;
import com.example.demo.entity.Gkpaperorder;
import com.example.demo.entity.Jkpaperorder;
import com.example.demo.enums.PayStateEnum;
import com.example.demo.exception.CustomException;
import com.example.demo.service.JkpaperorderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/jkpaperorder")
public class JkpaperorderController {
    @Resource
    private JkpaperorderService jkpaperorderService;

    @PostMapping("/list/page")
    public Result listJkpaperorderByPage(@RequestBody JkpaperorderQueryRequest jkpaperorderQueryRequest) {
        long current = jkpaperorderQueryRequest.getCurrent();
        long size = jkpaperorderQueryRequest.getPageSize();
        if (current <= 0) {
            current = 1;
        }
        if (size <= 0) {
            size = 10;
        }

        // 查询数据库
        Page<Jkpaperorder> orderPage = jkpaperorderService.page(new Page<>(current, size),
                jkpaperorderService.getQueryWrapper(jkpaperorderQueryRequest));
        return Result.success(orderPage);
    }

    @GetMapping("/get")
    public Result getJkpaperorder( JkpaperorderQueryRequest jkpaperorderQueryRequest) {
        long current = jkpaperorderQueryRequest.getCurrent();
        long size = jkpaperorderQueryRequest.getPageSize();
        Page<Jkpaperorder> jkpaperorderPage = jkpaperorderService.page(new Page<>(current, size),
                jkpaperorderService.getQueryWrapper(jkpaperorderQueryRequest));
        return Result.success(jkpaperorderPage);
    }
    @PostMapping("/add")
    public Result addJkpaperorder(@RequestBody Jkpaperorder jkpaperorder) {
        // 获取当前日期
        Date currentDate = new Date();
        jkpaperorder.setCreatetime(currentDate);

        SnowFlake idWorker = new SnowFlake(0, 0);
        String number = String.valueOf(idWorker.nextId());
        // 获取订单编号
        jkpaperorder.setNumber("GKP"+number);

        // 保存订单
        boolean result = jkpaperorderService.save(jkpaperorder);
        return result ? Result.success("添加成功") : Result.error("添加失败");
    }

    @DeleteMapping("/delete")
    public Result deleteJkpaperkorder(Integer id) {
        if (id == null || id <= 0) {
            throw new CustomException("参数异常");
        }
        boolean result = jkpaperorderService.removeById(id);
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }
    @PostMapping("/edit")
    public Result editJkpaperkorder(@RequestBody Jkpaperorder jkpaperorder) {
        // 获取当前日期
        Date currentDate = new Date();
        jkpaperorder.setCreatetime(currentDate);
        boolean result = jkpaperorderService.updateById(jkpaperorder);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

    @PostMapping("/refund")
    public Result refundJkpaperkorder(@RequestBody Jkpaperorder jkpaperorder) {

        if (jkpaperorder.getId() == null || jkpaperorder.getId() <= 0) {
            throw new CustomException("参数异常");
        }
        Jkpaperorder oldorder = jkpaperorderService.getById(jkpaperorder.getId());
        if (oldorder == null) {
            throw new CustomException("订单不存在");
        }
        if (oldorder.getState().equals(PayStateEnum.REFUND.getValue())) {
            throw new CustomException("订单已退款");
        }
        if (oldorder.getState().equals(PayStateEnum.WAIT_PAY.getValue())) {
            throw new CustomException("订单待支付");
        }
        if (oldorder.getState().equals(PayStateEnum.UNPAID.getValue())) {
            throw new CustomException("订单未支付");
        }

        if (oldorder.getState().equals(PayStateEnum.PAID.getValue())) {
            // 获取当前日期
            Date currentDate = new Date();
            oldorder.setCreatetime(currentDate);
            oldorder.setState(PayStateEnum.REFUND.getValue());
        }

        boolean result = jkpaperorderService.updateById(oldorder);
        return result ? Result.success("退款成功") : Result.error("退款失败");
    }



}
